﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_SystemInfo_OnlineUserManager :PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
    private int PageSize = 60;
    ViewQuery opViewQuery = new ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("SysOnlineUserManager_TitleInfo", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_UserName.Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_UserLoginIP.Text = GetLanageValueByKey("lbl_OnlineUser_LoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        
        base.InitPage();
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();

        if (!string.IsNullOrEmpty(txt_UserName.Text))
        {
            //sbSearch.Append(string.Format("((FKOnlineuserUserID in ( select FKStudentUserId from TStudent where StudentName like '%{0}%'))  or (FKOnlineuserUserID in ( select FKTeacherUserId from TTeacher where TeacherName like '%{0}%'))) and ", txt_UserName.Text.Trim()));
            sbSearch.Append(string.Format("((FKOnlineuserUserID in ( select Learner_Id from TO_Learner where UserName like '%{0}%'))  or (FKOnlineuserUserID in ( select AUser_Id from TO_AdvancedUser where Ausername like '%{0}%'))) and ", txt_UserName.Text.Trim()));

        }
        if (!string.IsNullOrEmpty(txt_UserLoginIP.Text))
        {
            sbSearch.Append(string.Format(" OnlineuserLoginIp like '%{0}%' and ", txt_UserLoginIP.Text.Trim()));
        }
        sbSearch.Append(" 1=1 ");
        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = opUser.QueryOnlineUserCount(search);
        AspNetPager1.PageSize = PageSize;
        PreAspNetPager(AspNetPager1);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        string searchA = "select * from TO_Learner";
        string searchB = "select * from TO_AdvancedUser";
        
        DataTable dt = new DataTable();
        DataTable dtLearner = opViewQuery.Query(searchA);
        DataTable dtAdvancedUser = opViewQuery.Query(searchB);

        string orderList = " OnlineuserLoginTime desc ";
        dt = opUser.QueryOnlineUser(search, orderList, PageSize, pageIndex);

        dt.Columns.Add("UserName1");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (!string.IsNullOrEmpty(dt.Rows[i]["FKOnlineuserUserId"].ToString()))
            {
                //PowerCreator.LMS.User.Entity.VOTTeacher voteacher = opUser.LoadTeacher(int.Parse(dt.Rows[i]["FKOnlineuserUserId"].ToString()));
                //if (voteacher.TeacherId > 0)
                //{
                    //if (voUser.UserType == 2)
                    //{
                    //    dt.Rows[i]["UserName1"] = GetLanageValueByKey("sys_onlineusermanager_admin1", PowerCreator.LMS.Language.LanagetItemType.Page);
                    //}
                    //else if (voUser.UserType == 3)
                    //{
                    //    dt.Rows[i]["UserName1"] = GetLanageValueByKey("sys_onlineusermanager_neizhi", PowerCreator.LMS.Language.LanagetItemType.Page);
                    //}
                    //else
                    //{
                    //    dt.Rows[i]["UserName1"] = opUser.GetRealNameByUserID(voUser.UserId);
                    //}
                    //dt.Rows[i]["UserName1"] = voteacher.TeacherName;
               // }
                if (dtLearner.Rows.Count > 0 && int.Parse(dt.Rows[i]["OnlineuserType"].ToString()) == 2)
                {
                    for (int j = 0; j < dtLearner.Rows.Count;j++ )
                    {
                        if (int.Parse(dt.Rows[i]["FKOnlineuserUserId"].ToString()) == int.Parse(dtLearner.Rows[j]["Learner_Id"].ToString()))
                        {
                            dt.Rows[i]["UserName1"] = dtLearner.Rows[j]["UserName"].ToString();
                        }
                    }
                }
                if (dtAdvancedUser.Rows.Count > 0 && int.Parse(dt.Rows[i]["OnlineuserType"].ToString()) == 1)
                {
                    for (int j = 0; j < dtAdvancedUser.Rows.Count; j++)
                    {
                        if (int.Parse(dt.Rows[i]["FKOnlineuserUserId"].ToString()) == int.Parse(dtAdvancedUser.Rows[j]["AUser_Id"].ToString()))
                        {
                            dt.Rows[i]["UserName1"] = dtAdvancedUser.Rows[j]["AUserName"].ToString();
                        }
                    }
                }

            }
        }
        
        rpt_UserOnline.DataSource = dt;
        rpt_UserOnline.DataBind();
    }

    protected void rpt_UserOnline_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Text = GetLanageValueByKey("btn_ManagerQuestion_Delete", PowerCreator.LMS.Language.LanagetItemType.Page);
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        } 
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_CurrentSearch_Click(object sender, EventArgs e)
    {
        
    }
    protected void rpt_UserOnline_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "ldel")
        {

            if (opUser.DelOnlineUser(int.Parse(e.CommandArgument.ToString())))
            {
                string search = GetSearch();
                BinderPager(search);
                BinderRPT(search, AspNetPager1.CurrentPageIndex);
            }
        }
    }
}
